var express = require("express");
var fortune = require("./lib/fortune.js");

var app = express();


var handlebars = require("express3-handlebars").create({
  defaultLayout: "main",
});
app.engine("handlebars", handlebars.engine);
//注意大小写
app.set("view engine", "handlebars");

app.set("port", process.env.PORT || 3000);

app.use(express.static(__dirname + "/public"));

app.use((req, res, next) => {
  res.locals.showTests =
    app.get("env") !== "production" && req.query.test === "1";
  next();
});

app.get("/", (req, res) => {
  // res.type('text/plain')
  // res.end('Meadewalk TRval')
  res.render("home");
});

app.get("/about", (req, res) => {
  // res.type('text/plain')
  // res.send('About Meadowlark TRval')
  //   var randomFortune = fortunes[Math.floor(Math.random()*fortunes.length)]
  res.render("about", {
    fortune: fortune.getFortune(),
    pageTestScript: "/qa/tests-about.js",
  });
});

app.get("/home", (req, res) => {
  res.render("home");
});

app.get("/tours/hood-river", function (req, res) {
  res.render("tours/hood-river");
});
app.get("/tours/request-group-rate", function (req, res) {
  res.render("tours/request-group-rate");
});

//添加中间件的一种方法app.use
app.use((req, res) => {
  res.type("text/plain");
  res.status(404);
  //   res.end("404-NOT Found");
  res.render("404");
});

app.use((err, req, res, next) => {
  console.log(err.stack);
  res.type("text/plain");
  res.status(500);
  //   res.end("500 - Serve Error");
  res.render("500");
});

app.listen(app.get("port"), () => {
  console.log(
    "Express started on http://localhost:" +
      app.get("port") +
      "; press Ctrl-C to terminate."
  );
});
